<template>
  <el-dialog
    v-model="visible"
    :title="title"
    width="400px"
    :before-close="handleClose"
    class="global-confirm"
  >
    <div class="confirm-content">
      <div class="confirm-icon">
        <el-icon :class="iconClass">
          <component :is="iconComponent" />
        </el-icon>
      </div>
      <div class="confirm-message">
        <p>{{ message }}</p>
        <p v-if="subMessage" class="sub-message">{{ subMessage }}</p>
      </div>
    </div>
    
    <template #footer>
      <div class="confirm-actions">
        <el-button @click="handleCancel">{{ cancelText }}</el-button>
        <el-button 
          :type="confirmType" 
          @click="handleConfirm"
          :loading="loading"
        >
          {{ confirmText }}
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref, computed } from 'vue'
import { 
  Warning, 
  InfoFilled, 
  CircleCheck, 
  CircleClose,
  QuestionFilled
} from '@element-plus/icons-vue'

// Props
const props = defineProps({
  modelValue: {
    type: Boolean,
    default: false
  },
  title: {
    type: String,
    default: '确认操作'
  },
  message: {
    type: String,
    required: true
  },
  subMessage: {
    type: String,
    default: ''
  },
  type: {
    type: String,
    default: 'warning',
    validator: (value) => ['warning', 'info', 'success', 'error', 'question'].includes(value)
  },
  confirmText: {
    type: String,
    default: '确认'
  },
  cancelText: {
    type: String,
    default: '取消'
  },
  loading: {
    type: Boolean,
    default: false
  }
})

// Emits
const emit = defineEmits(['update:modelValue', 'confirm', 'cancel'])

// 响应式数据
const visible = computed({
  get: () => props.modelValue,
  set: (value) => emit('update:modelValue', value)
})

// 计算属性
const iconComponent = computed(() => {
  const iconMap = {
    warning: Warning,
    info: InfoFilled,
    success: CircleCheck,
    error: CircleClose,
    question: QuestionFilled
  }
  return iconMap[props.type] || Warning
})

const iconClass = computed(() => {
  return `icon-${props.type}`
})

const confirmType = computed(() => {
  const typeMap = {
    warning: 'warning',
    info: 'primary',
    success: 'success',
    error: 'danger',
    question: 'primary'
  }
  return typeMap[props.type] || 'primary'
})

// 方法
const handleConfirm = () => {
  emit('confirm')
}

const handleCancel = () => {
  emit('cancel')
  visible.value = false
}

const handleClose = () => {
  if (!props.loading) {
    handleCancel()
  }
}
</script>

<style lang="scss" scoped>
.global-confirm {
  :deep(.el-dialog__header) {
    padding: 20px 20px 10px 20px;
    border-bottom: 1px solid var(--el-border-color-lighter);
  }
  
  :deep(.el-dialog__body) {
    padding: 20px;
  }
  
  :deep(.el-dialog__footer) {
    padding: 10px 20px 20px 20px;
    border-top: 1px solid var(--el-border-color-lighter);
  }
}

.confirm-content {
  display: flex;
  align-items: flex-start;
  gap: 16px;
}

.confirm-icon {
  flex-shrink: 0;
  
  .el-icon {
    font-size: 24px;
    
    &.icon-warning {
      color: var(--el-color-warning);
    }
    
    &.icon-info {
      color: var(--el-color-info);
    }
    
    &.icon-success {
      color: var(--el-color-success);
    }
    
    &.icon-error {
      color: var(--el-color-danger);
    }
    
    &.icon-question {
      color: var(--el-color-primary);
    }
  }
}

.confirm-message {
  flex: 1;
  
  p {
    margin: 0;
    font-size: 14px;
    line-height: 1.5;
    color: var(--el-text-color-primary);
  }
  
  .sub-message {
    margin-top: 8px;
    font-size: 13px;
    color: var(--el-text-color-secondary);
  }
}

.confirm-actions {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
}
</style>
